package com.hjx.windowtool.xiaoshuo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

public class XiaoshuoTest {
	public static void main(String[] args) throws Exception {
		
//		for (int i = 0; i < 100; i++) {
//			Connection conn = Jsoup.connect("https://m.kayege.com/book/57213/860.html");
//			conn.header("User-Agent",
//					"Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; MI 8 Build/OPM1.171019.026) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.4.0.1020 Mobile Safari/537.36");
//			Document d = conn.get();
//			String title = d.select(".page-title").text();
//			System.out.println(title);
//			Elements select = d.select(".page-content");
//			if (select.text().contains("暂时无内容")) {
//				System.out.println("暂时无内容");
//				Thread.sleep(10000);
//				continue;
//			}
//			System.out.println(select);
//			System.out.println(i);
//			break;
//		}
		
//		loadLzzxs("e://1.txt","http://m.lzzxs.com/book/796/",860,1128,true,100);
//		loadLzzxs("e://2.txt","http://m.lzzxs.com/book/7246/",531,1128,true,100);
//		loadLzzxs("e://3.txt","https://m.kayege.com/book/42250/",230,2128,true,30,10);
		loadLzzxs("e://4.txt","http://m.lzzxs.com/book/4117/",1,1,true,30,10);
	}
	public static class System{
		public static System out=new System() {};
		public void println(String s){
			java.lang.System.out.println(s);
		}
		public void println(Object s){println(String.valueOf(s));}
	}
	public static void loadLzzxs(String url,int start,int end,boolean isAdd,int errorSize,int emptySize) throws Exception {
		Connection conn = Jsoup.connect(url);
		conn.header("User-Agent",
				"Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; MI 8 Build/OPM1.171019.026) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.4.0.1020 Mobile Safari/537.36");
		Document d = conn.get();
//		System.out.println(d);
		Elements elements = d.getElementsByTag("h2");
		String name = elements.get(0).text()+".txt";
		System.out.println(name);
		if(start==1) {
			if(new File(name).exists()) {
				new File(name).delete();
			}
		}
		loadLzzxs(name, url, start, end, isAdd, errorSize, emptySize);
	}

	/**
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void loadLzzxs(String path,String url,int start,int end,boolean isAdd,int errorSize,int emptySize) throws Exception {
		File f = new File(path);
		
//		Files.write(f.toPath(), Arrays.asList("1","2","3","4","5"), StandardOpenOption.APPEND,StandardOpenOption.CREATE);
		Map<Integer,Integer> mapErrorSize=new HashMap<>();
		int esize=0;
		for (int i = start; i <=end; i++) {
			try {
				Connection conn = Jsoup.connect(url+i+".html");
				conn.header("User-Agent", "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; MI 8 Build/OPM1.171019.026) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.4.0.1020 Mobile Safari/537.36");
				Document d = conn.get();
//			System.out.println(d);
				String title = d.select(".page-title").text();
				System.out.println(i+"===="+title);
//				System.out.println(d);
				List<String> eachText = new ArrayList<>();
				if (isAdd) {
					title=i+"章 "+title;
				}
				eachText.add(title);
				Elements select = d.select(".page-content");
				List<Element> childNodes = select.get(0).children();
				
				List<Element> tagName = childNodes;
				if(tagName.size()>0) {
					tagName.remove(tagName.size()-1);
				}
				for (Element element : tagName) {
					String text = element.text();
//					System.out.println(text);
					if(text==null||text.trim().isEmpty()){
						continue;
					}
					eachText.add(text);
				}
				if(eachText.toString().contains("暂时无内容")) {
					Integer size = mapErrorSize.getOrDefault(i, 0);
					System.out.println(size+"==="+errorSize+"==="+esize+"==="+emptySize);
					if(size>errorSize) {
						esize++;
						if(esize>emptySize) {
							break;
						}
						continue;
					}
					mapErrorSize.put(i, size+1);
					Thread.sleep(10000);
					i--;
					continue;
				}
//			System.out.println(eachText);
				
				
				conn = Jsoup.connect(url+i+"_2.html");
				conn.header("User-Agent", "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; MI 8 Build/OPM1.171019.026) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.4.0.1020 Mobile Safari/537.36");
				d = conn.get();
//				System.out.println(d);
				select = d.select(".page-content");
				childNodes = select.get(0).children();
				
				tagName = childNodes;
				for (Element element : tagName) {
					String text = element.text();
					if(text==null||text.equals(eachText.get(eachText.size()-1))) {
						continue;
					}
//					System.out.println(text);
					eachText.add(text);
				}
//				System.out.println(eachText.get(0));
				
				Files.write(f.toPath(), eachText, StandardOpenOption.APPEND,StandardOpenOption.CREATE);
				Thread.sleep(1000);
				if(i%50==0) {
					Thread.sleep(10000);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Thread.sleep(10000);
				i--;
			}
		}
	}
}
